/*
    A program for the MSP430FG4618 on the Experimenter's board to accept I2C information from the touchpad,
    display it numerically on the LCD, and send it to a host PC for graphical display.
    
    Steve Underwood 
*/

#include <stdint.h>
#include <msp430xG46x.h>
#include "FG4618 host_comms.h"


uint8_t valueToLCD = 0; //value to display on LCD

void main(void)
{

    uint8_t valueFromFlash = 0; 

    uint16_t* flashPtrForRead;
    flashPtrForRead = (uint16_t*)0x01800;

    WDTCTL = WDTPW | WDTHOLD;           /* Stop watchdog */
    
    configureUartUsci0();
    configureI2cUsci0();
    
    PortsInit();			// Initialize ports
    initLCD();
    
    valueFromFlash = ReadFromInfoMem(flashPtrForRead);  //read from flash
    LCDdec(valueFromFlash, 3);  //display value retrieved from flash         

    _EINT();
    
    for (;;)
    {
        /* Normal operation */
        //use TACCR0 to request interrupt after 15sec pause in touching touchpad
        TACCR0 = DELAY15sec;  //"compare" value for 15sec delay
        TACCTL0 = CCIE; //Enable interrupts on Compare 0
        //Start timer: ACLK, divide by 8, continuous mode, clear
        TACTL = TASSEL_1 | ID_3 | MC_2 | TACLR ; 
        writeToInfoMem(valueToLCD); //write value to flash  
        LPM0;
        LCDdec(valueToLCD, 3);
    }
}

#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
    valueToLCD =
    UCA0TXBUF = UCB0RXBUF;
}

#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR(void)
{
    UCB0STAT &= ~(UCSTPIFG | UCSTTIFG);
    LPM0_EXIT;
}

#pragma vector = TIMERA0_VECTOR
__interrupt void timera0_interrupt(void)
{
    clrLCD(); //clear LCD display
    TACCTL0 = ~CCIE;  //disable further interrupts on CCIFG0
    TACTL &= ~MC_3; //stop timer
}

